package com.gnerv.cms.controller;

import com.gnerv.cms.common.Result;
import com.gnerv.cms.dto.DictItemDTO;
import com.gnerv.cms.dto.DictItemSaveDTO;
import com.gnerv.cms.service.IDictItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 基础平台-字典项 controller 接口
 *
 * @author zhang
 * @date 2019/12/2
 **/
@Api(tags = "字典项管理")
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/api/manage/dict/item")
public class DictItemController {

    @Resource
    private IDictItemService deleteDictItem;

    @RequestMapping(value = "save", method = RequestMethod.POST)
    public Result saveDictItem(@RequestBody DictItemSaveDTO dictItemSaveDTO) {
        deleteDictItem.saveDictItem(dictItemSaveDTO);
        return Result.response();
    }

    @RequestMapping(value = "create", method = RequestMethod.POST)
    public Result createDictItem(@RequestBody DictItemSaveDTO dictItemSaveDTO) {
        deleteDictItem.createDictItem(dictItemSaveDTO);
        return Result.response();
    }

    @RequestMapping(value = "delete/{dictItemId}", method = RequestMethod.GET)
    public Result deleteDictItem(@PathVariable(value = "dictItemId") String dictItemId) {
        deleteDictItem.deleteDictItem(dictItemId);
        return Result.response();
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    public Result updateDictItem(@RequestBody DictItemSaveDTO dictItemSaveDTO) {
        deleteDictItem.updateDictItem(dictItemSaveDTO);
        return Result.response();
    }

    @RequestMapping(value = "/{dictItemId}/status/{status}", method = RequestMethod.GET)
    public Result updateDictItemStatus(@PathVariable(value = "dictItemId") String dictItemId, @PathVariable(value = "status") Integer status) {
        deleteDictItem.updateDictItemStatus(dictItemId, status);
        return Result.response();
    }

    @RequestMapping(value = "list/{dictId}", method = RequestMethod.GET)
    public Result listDictItem(@PathVariable(value = "dictId") String dictId) {
        List<DictItemDTO> dictItems = deleteDictItem.listDictItemByDictId(dictId);
        return Result.response().data("dictItems", dictItems);
    }

}

